# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: reporter\reports\exporter_fortinet_vulnerabilities.py
from reporter.reports.basic_template import NamedTempFileWithNormalizedWrite
from reporter.reports.basic_exporter import BasicExporter
from io import StringIO
import jinja2, datetime


class ExporterFortinetVulnerabilities(BasicExporter):
    severity_colour = ["green", "blue", "orange", "red"]

    def parts(self):
        template_loader = jinja2.FileSystemLoader(searchpath="data/templates/reports/")
        template_env = jinja2.Environment(loader=template_loader)
        template_vars = {}
        template = template_env.get_template(
            "exporter_fortinet_vulnerabilities_template.xml"
        )
        with NamedTempFileWithNormalizedWrite() as (file_report_items):
            data = StringIO()
            template_vars["template_part"] = "document_begin"
            template_vars["exported_date"] = self.get_formatted_date(
                datetime.datetime.now()
            )
            data.write(self.remove_blank_lines(template.render(template_vars)))
            data.seek(0)
            yield data
            data = StringIO()
            for source in self.sources:
                data = StringIO()
                count = 0
                for (
                    vulnerability,
                    location,
                    vulnerability_type,
                ) in source.vulnerabilities():
                    count += 1
                    template_vars["count"] = count
                    vulnerability_type["cwe_list"] = self.extract_cwe_list(
                        vulnerability_type.get("tags")
                    )
                    vulnerability_type["cve_list"] = self.extract_cve_list(
                        vulnerability_type.get("tags")
                    )
                    vulnerability_type["cvss2_decoded"] = self.decode_cvss(
                        vulnerability_type["cvss2"], "cvss2"
                    )
                    if vulnerability_type["cvss3"]:
                        vulnerability_type["cvss3_decoded"] = self.decode_cvss(
                            vulnerability_type["cvss3"], "cvss3"
                        )
                    vulnerability_type["severity_string"] = self.get_severity_string(
                        vulnerability_type["severity"]
                    )
                    template_vars["vulnerability_type"] = vulnerability_type
                    vulnerability["location"] = location
                    vulnerability["color"] = self.severity_colour[
                        vulnerability_type["severity"]
                    ]
                    vulnerability["request"] = self.escape_html(
                        vulnerability.get("request") or ""
                    )
                    vulnerability["attack_vector"] = self.normalize_encoding(
                        vulnerability.get("attack_vector") or ""
                    )
                    template_vars["vulnerability"] = vulnerability
                    template_vars["template_part"] = "report_item"
                    file_report_items.write(
                        self.remove_blank_lines(template.render(template_vars))
                    )

                template_vars["scan_info"] = self.get_well_formed_scan_info(source)
                template_vars["exported_date"] = self.get_formatted_date(
                    datetime.datetime.now()
                )
                template_vars["scan_name"] = "scan_name"
                template_vars["scan_short_name"] = "scan_short_name"
                file_report_items.seek(0)
                template_vars["report_items"] = file_report_items.read()
                template_vars["template_part"] = "export_structure"
                data.write(self.remove_blank_lines(template.render(template_vars)))
                data.seek(0)
                yield data
                data = StringIO()

            template_vars["template_part"] = "document_end"
            data.write(self.remove_blank_lines(template.render(template_vars)))
            data.seek(0)
            yield data
